package org.bigjava.buff.user.web.interceptor;

import org.bigjava.buff.user.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserLoginInterceptor implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取用户
        User user = (User) request.getSession().getAttribute("userNow");


        //用户已登陆 放行
        if(user != null)  {
            return true;
        }


        //用户未登陆
//        response.sendRedirect("/index.jsp");
        request.getRequestDispatcher("index.jsp");
//        request.getSession().setAttribute("state", 1);

//        request.getRequestDispatcher("user/gotoLogin").forward(request, response);

        //登录请求放行
//        if(request.getRequestURI().contains("user/gotoLogin")) return true;



//        request.getRequestDispatcher("/goods/showBox").forward(request, response);
        //用户未登陆

        return false;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}


